package com.office.anywher.poly.list.email;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.office.anywher.R;
import com.office.anywher.execeptions.OAException;
import com.office.anywher.https.HttpClientService;
import com.office.anywher.poly.list.Common;
import com.office.anywher.poly.list.IDataReceiveForList;
import com.wenxy.common.IConst;

public class ListInBoxEmail extends Common implements IDataReceiveForList{
	private int mType;
	public ListInBoxEmail(int type){
		mType = type;
	}
	@Override
	public List<HashMap<String, Object>> receive(HttpClientService aService,int start,int rowNum) throws OAException {
		List<HashMap<String, Object>> rtnList = new ArrayList<HashMap<String, Object>>();
		JSONArray[] jsonarrays = new JSONArray[]{new JSONArray(),new JSONArray()}; 
		jsonarrays[0] = aService.getEmailList(IConst.Http.Email.InBox.URL,null, start, rowNum);
		if(jsonarrays !=null && jsonarrays.length > 0){
			for(JSONArray aJsonArray:jsonarrays){
				if(aJsonArray==null || aJsonArray.length()<=0)continue;
				for(int i = 0;i<aJsonArray.length();i++){
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject aJsoObj = null;
					try {
						aJsoObj = aJsonArray.getJSONObject(i);
						if(aJsoObj == null)continue;
						/*if(isNullInJsonObject(aJsoObj,IConst.Http.Email.InBox.OUT.STAT) && mType == IConst.Http.Email.InBox.TYPE_1)continue;
						int stat = aJsoObj.getInt(IConst.Http.Email.InBox.OUT.STAT);
						if(mType == IConst.Http.Email.InBox.TYPE_1){
							if(stat != IConst.Http.Email.InBox.TYPE_1_S[0] && IConst.Http.Email.InBox.TYPE_1_S[1] != stat)continue;
						}else if(mType == IConst.Http.Email.InBox.TYPE_5){
							if(stat != IConst.Http.Email.InBox.TYPE_5_S[0] && IConst.Http.Email.InBox.TYPE_5_S[1] != stat)continue;
						}*/
						if(!isNullInJsonObject(aJsoObj,IConst.Http.Email.InBox.OUT.STAT)){
							if(aJsoObj.getInt(IConst.Http.Email.InBox.OUT.STAT) == IConst.Http.Email.READED){
								map.put(IConst.Http.Email.InBox.OUT.ITEM_IMAGE, R.drawable.email_readed);
							}else if(aJsoObj.getInt(IConst.Http.Email.InBox.OUT.STAT) == IConst.Http.Email.UNREAD){
								map.put(IConst.Http.Email.InBox.OUT.ITEM_IMAGE, R.drawable.email_unread);
							}else{
								map.put(IConst.Http.Email.InBox.OUT.ITEM_IMAGE, R.drawable.email_unread);
							}
						}else{
							map.put(IConst.Http.Email.InBox.OUT.ITEM_IMAGE, R.drawable.email_unread);
						}
						map.put(IConst.Http.Email.InBox.OUT.STAT,isNullInJsonObject(aJsoObj,IConst.Http.Email.InBox.OUT.STAT)?"  ":aJsoObj.getString(IConst.Http.Email.InBox.OUT.STAT));
						map.put(IConst.Http.Email.InBox.OUT.SENDER,isNullInJsonObject(aJsoObj,IConst.Http.Email.InBox.OUT.SENDER)?"  ":aJsoObj.getString(IConst.Http.Email.InBox.OUT.SENDER));
						map.put(IConst.Http.Email.InBox.OUT.SEND_TIME,isNullInJsonObject(aJsoObj,IConst.Http.Email.InBox.OUT.SEND_TIME)?"  ":aJsoObj.getString(IConst.Http.Email.InBox.OUT.SEND_TIME));
						map.put(IConst.Http.Email.InBox.OUT.EMAIL_TITLE,isNullInJsonObject(aJsoObj,IConst.Http.Email.InBox.OUT.EMAIL_TITLE)?"  ":aJsoObj.getString(IConst.Http.Email.InBox.OUT.EMAIL_TITLE));
						map.put(IConst.Http.Email.InBox.OUT.ID,isNullInJsonObject(aJsoObj,IConst.Http.Email.InBox.OUT.ID)?"  ":aJsoObj.getString(IConst.Http.Email.InBox.OUT.ID));
						if(!isNullInJsonObject(aJsoObj,IConst.Http.Email.InBox.OUT.ATTACH)){
							if(aJsoObj.getBoolean(IConst.Http.Email.InBox.OUT.ATTACH)){
								map.put(IConst.Http.Email.InBox.OUT.ATTACH, R.drawable.attach);
							}else{
								map.put(IConst.Http.Email.InBox.OUT.ATTACH, R.drawable.use_empty);
							}
						}else{
							map.put(IConst.Http.Email.InBox.OUT.ATTACH, R.drawable.use_empty);
						}
						rtnList.add(map);
					} catch (JSONException e) {
						e.printStackTrace();
						throw new OAException(OAException.OA_EXECEPTION_DEF[2]);
					}
				}
			}
		}
		return rtnList;
	}
	
	public int getEmailNumber(HttpClientService aService) throws OAException{
		JSONArray[] jsonarrays = new JSONArray[]{new JSONArray(),new JSONArray()}; 
		jsonarrays[0] = aService.getEmailList(IConst.Http.Email.InBox.URL,null, 0, 0);
		int count = 0;
		if(jsonarrays !=null && jsonarrays.length > 0){
			for(JSONArray aJsonArray:jsonarrays){
				if(aJsonArray==null || aJsonArray.length()<=0)continue;
				for(int i = 0;i<aJsonArray.length();i++){
					JSONObject aJsoObj = null;
					try {
						aJsoObj = aJsonArray.getJSONObject(i);
						if(aJsoObj == null)continue;
						if(!isNullInJsonObject(aJsoObj,IConst.Http.Email.InBox.OUT.STAT))
							if(aJsoObj.getInt(IConst.Http.Email.InBox.OUT.STAT) == IConst.Http.Email.UNREAD)
								count++;						
					} catch (JSONException e) {
						e.printStackTrace();
						throw new OAException(OAException.OA_EXECEPTION_DEF[2]);
					}
				}
			}
			}
		return count;
	}
}
